package com.prs.crm.action.oa;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MyMailList;
import com.prs.crm.domain.oa.MyMailListType;
import com.prs.crm.domain.oa.PublicMailList;
import com.prs.crm.domain.oa.PublicMailListType;
import com.prs.crm.service.oa.MyMailListService;
import com.prs.crm.service.oa.MyMailListTypeService;
import com.prs.crm.service.oa.PublicMailListService;
import com.prs.crm.service.oa.PublicMailListTypeService;

public class PublicMailListAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	private PublicMailList publicMailList;
	private PublicMailListService publicMailListService;
	private List<PublicMailListType> publicMailListType;
	private PublicMailListTypeService publicMailListTypeService;
	private String birthday;
	
	@Action("listPublicMailList")
	public String listPublicMailList(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PublicMailList.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPublicMailListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("selectType")
	public String selectType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PublicMailListType.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPublicMailListTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("editPublicMailList")
	public String editPublicMailList(){
		publicMailListType=this.getPublicMailListTypeService().getAllPublicMailListType();
		return SUCCESS;
	}
	
	@Action(value = "removePublicMailList", results = {
			@Result(name = "success", type = "redirect", location = "listPublicMailList"),
			@Result(name = "error", type = "chain", location = "listPublicMailList") })
	public String removePublicMailList() {
		if (ids == null) {
			this.addActionError("请选择要删除的通讯录");
			return ERROR;
		}
		this.getPublicMailListService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "savePublicMailList", results = {
			@Result(name = "success", type = "redirect", location = "listPublicMailList"),
			@Result(name = "error", type = "chain", location = "editPublicMailList") })
	public String savePublicMailList() {
		if (publicMailList.getName() == null || publicMailList.getName().trim().equals("")) {
			this.addActionError("请输入联系人姓名");
			return ERROR;
		}
		if(publicMailList.getPublicMailListType() == null){
			this.addActionError("请选择所在类型");
			return ERROR;
		}
		Person person = AppContext.getAccountContext().currentUser();
		publicMailList.setPerson(person);
		publicMailList.setBirthday(birthday);
		this.getPublicMailListService().save(publicMailList);
		return SUCCESS;
	}
	
	@Action("viewPublicMailList")
	public String viewPublicMailList(){
		return ERROR;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}


	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public PublicMailList getPublicMailList() {
		return publicMailList;
	}

	public void setPublicMailList(PublicMailList publicMailList) {
		this.publicMailList = publicMailList;
	}

	public PublicMailListService getPublicMailListService() {
		return publicMailListService;
	}

	public void setPublicMailListService(PublicMailListService publicMailListService) {
		this.publicMailListService = publicMailListService;
	}

	public PublicMailListTypeService getPublicMailListTypeService() {
		return publicMailListTypeService;
	}

	public void setPublicMailListTypeService(
			PublicMailListTypeService publicMailListTypeService) {
		this.publicMailListTypeService = publicMailListTypeService;
	}

	public List<PublicMailListType> getPublicMailListType() {
		return publicMailListType;
	}

	public void setPublicMailListType(List<PublicMailListType> publicMailListType) {
		this.publicMailListType = publicMailListType;
	}
}
